using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class ImagesXml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime? version = DateTime.Now;
        if (Request.QueryString["AcmsSV"] != null)
        {
            if (Request.QueryString["AcmsSV"].ToLower() == "pending")
            {
                version = null;
            }
            else
            {
                version = DateTime.Parse(Request.QueryString["AcmsSV"]);
            }
        }

        string controlName = Request.QueryString["AcmsControlName"];
        if (string.IsNullOrEmpty(controlName))
        {
            controlName = "Image Content";
        }

        string siteRoot = Request.Url.DnsSafeHost;
        if (Request.ApplicationPath != "/")
            siteRoot = string.Concat(siteRoot, Request.ApplicationPath);
        Acms.Bll.Site site = Acms.Bll.Site.GetByUrl(siteRoot);

        List<Acms.Bll.PostControl> controls = Acms.Bll.PostControl.GetAllBySiteIdAndTypeName(site.Id, controlName, version);

        Response.ContentType = "text/xml";
        XmlTextWriter xml = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);
        xml.WriteStartDocument();
        xml.WriteStartElement("images");

        foreach (Acms.Bll.PostControl control in controls)
        {
            if (control.Value != null)
            {

                XmlAttribute fileId = control.Value.DocumentElement.FirstChild.Attributes["resourceId"];
                XmlAttribute width = control.Value.DocumentElement.FirstChild.Attributes["width"];
                XmlAttribute height = control.Value.DocumentElement.FirstChild.Attributes["height"];

                if (fileId != null && !string.IsNullOrEmpty(fileId.Value))
                {
                    xml.WriteStartElement("image");
                    xml.WriteAttributeString("id", fileId.Value);
                    if (width != null && !string.IsNullOrEmpty(width.Value))
                        xml.WriteAttributeString("imgWidth", width.Value);
                    if (height != null && !string.IsNullOrEmpty(height.Value))
                        xml.WriteAttributeString("imgHeight", height.Value);
                    xml.WriteRaw(Page.ResolveUrl(string.Format("http://{0}/File.aspx?Id={1}", siteRoot, fileId.Value)));
                    xml.WriteEndElement();
                }
            }
        }

        xml.WriteEndElement();
        xml.WriteEndDocument();
        xml.Close();
        Response.Flush();
        Response.End();
    }
}
